<?php
/**
 * 本页面用于单个采集
 * 仅供测试用 
 */
include_once ('../sys/sys.php');
header ( 'Content-Type: text/html; charset=utf-8' );

// 預定義JS解析器變量
$tmpjs_path = "";

$sedtr = "Print( replace );";

$js_str = <<<EOT
	LoadModule('jsstd');
    LoadModule('jscrypt');
	
	function unsuan(s,a){
	    k=a.substring(0,a.length-1);
		f=a.substring(a.length-1);
	    for(i=0;i<k.length;i++){
	        eval("s=s.replace(/"+k.substring(i,i+1)+"/g,'"+i+"')")
	    }
	    var ss=s.split(f);
	    s="";
	    for(i=0;i<ss.length;i++){
	        s+=String.fromCharCode(ss[i])
	    }
	    return s
	}

	
EOT;

// 定服务器前缀
/*
 * $server_url_list = array("http://58.51.95.67:9896/dm01", "http://58.51.95.67:9896/dm02", "http://58.51.95.67:9896/dm03", "http://58.51.95.66:9898/dm04", "http://121.12.121.199:9199/dm05", "http://58.51.95.67:9896/dm06", "http://58.51.95.67:9896/dm07", "http://121.12.121.199:9199/dm08", "http://58.51.95.67:9896/dm09", "http://58.51.95.67:9896/dm10", "http://58.51.95.67:9896/dm11", "http://121.12.121.199:9199/dm12", "http://58.51.95.67:9896/dm13", "http://8.8.8.8:99/dm14", "http://58.51.95.67:9896/dm15", "http://121.12.121.199:9199/dm16" );
 */

// $server_url_list = array (
// "http://121.12.121.199:9199/dm01/",
// "http://58.51.95.67:9896/dm02/",
// "http://58.51.95.67:9896/dm03/",
// "http://121.12.121.199:9199/dm04/",
// "http://58.215.240.35:916/dm05/",
// "http://58.51.95.67:9896/dm06/",
// "http://121.12.121.199:9199/dm07/",
// "http://58.51.95.67:9896/dm08/",
// "http://58.215.240.35:916/dm09/",
// "http://58.215.240.35:916/dm10/",
// "http://58.51.95.67:9896/dm11/",
// "http://58.215.240.35:916/dm12/",
// "http://58.51.95.67:9896/dm13/",
// "http://8.8.8.8:99/dm14/",
// "http://58.215.240.35:916/dm15/",
// "http://58.51.95.67:9896/dm16/"
// );

$tid = $_GET ['tid'];
if (! is_numeric ( $tid ) || $tid < 0) {
	show_normal_mesR ( 'TAG ID错误', 'TAG ID错误', 'TAG查询' );
}

$url = 'http://www.hhcomic.com/comic/' . $tid . '/';

// 分离URL以及ID
$id = $tid;
// 定义公用变量
if (substr ( $url, - 1 ) != "/")
	$url .= "/";
$url_pre = 'http://www.hhcomic.com/';
$jsurl_pre = 'http://hhcomic.com/';

// 获取URL内容
$content = file_get_contents ( $url );
$content = mb_convert_encoding ( file_get_contents ( $url ), "UTF-8", "GB2312" );

// 获取名称
$name = explode ( 'valign="top" ><b>', $content );
$name = $name [1];
$name = explode ( '内容', $name );
$name = ADD_addslashes ( $name [0], true );

// 获取最后更新时间
$dt = time ();

// 获取URL列表
$urls = explode ( '<ul class="bi">', $content );
$urls = $urls [1];
// echo $urls;exit;
// $urls = explode('</ul>',$urls);
$urls = explode ( '<div style=', $urls );
$urls = $urls [0];

// echo $urls;exit;

$match_str = "/<li><a href=(.*) target=_blank>/isU";
preg_match_all ( $match_str, $urls, $urln );
$urln = $urln [1];
// 获取每单位总数
$ulnum = count ( $urln );
// 进入每页进行处理

// echo $ulnum;exit;

// 插入分类信息
// 检查当前ID是否存在
$where = 'AND id= ' . $id . '';
$result = $DB->build_and_exec_query ( array (
		'select' => 'COUNT(*) as max',
		'from' => 'hhtags',
		'where' => "1 " . $where 
) );
// 当已经存在的时候更新日期
if ($result ['max'] > 0) {
} // 当不存在时插入
else {
	do_taginsert ( $id, $dt, $name, 0 );
}
// 获取数据库COUNT
$where = 'AND id= ' . $id . '';
$result = $DB->build_and_exec_query ( array (
		'select' => 'count',
		'from' => 'hhtags',
		'where' => "1 " . $where 
) );
$dbcount = $result ['count'];

// 由于是逆序，需要倒装数组
$urln = array_reverse ( $urln );

foreach ( $urln as $k => $v ) {
	// 当COUNT小于已经采集的时候，跳过。
	if ($k < $dbcount) {
		continue;
	}
	$current_url = $url_pre . $v;
	$comic_contents = file_get_contents ( $current_url );
	// 获取关键JS内容
	$jjsjscontent = get_js_content ( $comic_contents, $url_pre );
	// 获取下载服务器连接
	$server_url_list = get_server_list ( $jjsjscontent );
	// 获取服务器列表号
	$serid = explode ( 's=', $v );
	$serid = $serid [1];
	// 构造图片前缀
	$pic_download_prex = make_pic_url_prex ( $serid, $server_url_list );
	
	echo $pic_download_prex;
	
	// 获取所有图片连接
	$purls = explode ( 'var PicListUrl = "', $comic_contents );
	// echo $purls[1];exit;
	$purls = explode ( '";', $purls [1] );
	// echo $purls[0];exit;
	$purls = mb_convert_encoding ( 'var PicListUrl = "' . $purls [0] . '";', "UTF-8", "GB2312" );
	// 构造JS代码
	// 根据不同的JS连接获取其JS内的散列关键字
	$unsuanjsjscontent = get_js_content ( $comic_contents, $url_pre );
	$junsuan_code = get_unsuan_code ( $unsuanjsjscontent );
	$jjs_str .= $js_str . trim ( $purls . "\r\n" . 'var pul=unsuan(PicListUrl,"' . $junsuan_code . '");' . "\r\nPrint(pul);" );
	$path_info = pathinfo ( $_SERVER ['SCRIPT_FILENAME'] );
	$tmpjs_path = $path_info ['dirname'] . "/1.js";
	unlink ( $tmpjs_path );
	file_put_contents ( $tmpjs_path, $jjs_str );
	$jjs_str = "";
	// 解码图片连接完毕，丢回去
	$ttt = exec ( "jshost " . $tmpjs_path );
	// 串行化数组
	$purls = serialize ( explode ( "|", $ttt ) );
	// 这里添加数据库插入语句
	do_photoinsert ( $id, $purls, $pic_download_prex );
}

// 更新计数
$where = 'id= ' . $id . '';
$DB->do_update ( 'hhtags', array (
		'lastupdate' => $dt,
		'count' => count ( $urln ) 
), $where );
function make_pic_url_prex($server_id, $server_list) {
	return $server_list [$server_id - 1];
}
function do_taginsert($id, $lu, $name, $count) {
	global $DB;
	$DB->do_insert ( 'hhtags', array (
			'id' => $id,
			'lastupdate' => $lu,
			'name' => $name,
			'count' => $count 
	) );
	if ($result === false) {
		show_normal_mesR ( '无法添加当前漫画', '添加出错', '添加' );
	}
}
function do_photoinsert($uid, $url, $prez) {
	global $DB;
	$DB->do_insert ( 'hhphotos', array (
			'uid' => $uid,
			'url' => $url,
			'prez' => $prez 
	) );
	if ($result === false) {
		show_normal_mesR ( '无法添加当前漫画', '添加出错', '添加' );
	}
}
// 漫画具体下载地址生成
function make_picurls($base_url, $pages, $prez) {
	$urls = "";
	get_pre_nums ( $prez, 1 );
	for($i = 1; $i < $pages + 1; $i ++) {
		$urls .= $base_url . get_pre_nums ( $prez, $i ) . ".jpg\r\n";
	}
	return $urls;
}
function get_unsuan_code($pjsjscontent) {
	// $jsurl = explode ( '/js.js', $comic_contents );
	// $jsurl = explode ( '/', $jsurl [0] );
	// $jsurl = $jsurl [count ( $jsurl ) - 1] . "/js.js";
	
	// switch ($jsjs_url){
	// case "/h/h.js":
	// break;
	// case "/hh/h.js":
	// break;
	// default:
	// return false;
	// break;
	// }
	// $jsjscontent = file_get_contents ( $url_pre . $jsurl );
	$unsuan_code = explode ( 'unsuan(PicListUrl,"', $pjsjscontent );
	$unsuan_code = explode ( '")', $unsuan_code [1] );
	$unsuan_code = $unsuan_code [0];
	return $unsuan_code;
}
function get_js_content($comic_contents, $jsurl_pre) {
	$jsurl = explode ( '/js.js', $comic_contents );
	$jsurl = explode ( '/', $jsurl [0] );
	$jsurl = $jsurl [count ( $jsurl ) - 1] . "/js.js";
	$jsjscontent = file_get_contents ( $jsurl_pre . $jsurl );
	return $jsjscontent;
}
function get_server_list($jsjscontent) {
	$surl = explode ( 'var s1=', $jsjscontent );
	$surl = explode ( 'var page', $surl [1] );
	$surl = explode ( 'ServerList[0]=', $surl [0] );
	$surl = str_replace ( array (
			"=",
			'ServerList[10',
			'ServerList[11',
			'ServerList[12',
			'ServerList[13',
			'ServerList[14',
			'ServerList[15',
			'ServerList[16',
			'ServerList[17',
			'ServerList[18',
			'ServerList[19',
			"ServerList[0",
			"ServerList[1",
			"ServerList[2",
			"ServerList[3",
			"ServerList[4",
			'ServerList[5',
			'ServerList[6',
			'ServerList[7',
			'ServerList[8',
			'ServerList[9',
			']',
			'"' 
	), "", $surl [1] );
	$surl = explode ( ';', $surl );
	
	return $surl;
	// print_r($surl);
	// exit ();
}

echo "<script>history.back();</script>";
echo "采集完毕";

exit ();

?>